import { storeToRefs } from 'pinia'
import { useMultipleUIStore } from '@/stores/multipleTaskUI.js'
import { useRoute } from 'vue-router'


import { useFormStore } from '@/stores/formData.js'
import { useSimpleUIStore } from '@/stores/simpleTaskUI.js'
import { useMultipleTaskData } from '@/stores/multipleTaskData.js'
import { useSimpleTaskData } from '@/stores/simpleTaskData.js'
import useUIStore from '@/stores/UI.js'

const multipleUIStore = useMultipleUIStore()
const { UIOnMultipleSubmit } = multipleUIStore

const formStore = useFormStore()

const simpleUIStore = useSimpleUIStore()
const { UIOnSimpleSubmit } = simpleUIStore

const multipleTaskData = useMultipleTaskData()
const { statisticalOption } = storeToRefs(multipleTaskData)

const simpleTaskData = useSimpleTaskData()

const uiStore = useUIStore()
const { isSimpleTask } = storeToRefs(uiStore)
import socket from '@/utils/socket.js'
export default function taskUploadHandler() {

  const handleTaskCallback = ref(() => {
  })
  const route = useRoute()
  // 上传到python的数据,用于运作指定的算法
  const uploadAlgorithmsData = reactive([])
  const uploadProblemData = reactive([])
// 处理上传的任务数据
  const handleParams = (list, Items, data) => {
    for (const item of Items) {
      const params = {}
      for (const param of list[item].params) {
        params[param.name] = param.defaultValue
      }
      data.push({
        ...list[item],
        params
      })
    }
  }
  const handleSimpleTask = () => {
    UIOnSimpleSubmit()
    simpleTaskData.saveHistory(uploadAlgorithmsData[0], uploadProblemData[0], formStore.formData)
    return { messageType: '5', indicator_data: simpleTaskData.indicators[simpleTaskData.indicator] }
  }
  const handleMultipleTask = () => {
    UIOnMultipleSubmit()
    // 清空右侧表格数据
    multipleTaskData.clearAllData()
    return { messageType: '1', indicator_data: multipleTaskData.indicators[multipleTaskData.indicator] }
  }
  const handleTaskObj = {
    simpleCharts: handleSimpleTask,
    multipleTables: handleMultipleTask
  }
  // 至少选择一个算法或者问题
  const formValidation = (data, errorInfo) => {
    if (data.length === 0) {
      ElMessage.error(errorInfo)
      return false
    }
    return true
  }

  const handleTaskFunc = ref(handleSimpleTask)

  const handleUploadTask = (algorithm_list, algorithm_Items, problem_list, problem_Items) => {
    if (!formValidation(algorithm_Items, '至少选择一个算法'))
      return
    if (!formValidation(problem_Items, '至少选择一个测试问题'))
      return
    //清空上传的任务数据
    uploadAlgorithmsData.splice(0)
    uploadProblemData.splice(0)
    handleParams(algorithm_list, algorithm_Items, uploadAlgorithmsData)
    handleParams(problem_list, problem_Items, uploadProblemData)
    const { messageType, indicator_data } = handleTaskFunc.value()
    const uploadData = {
      messageType,
      uploadAlgorithmsData,
      uploadProblemData,
      formData: formStore.formData,
      indicator_data,
      statistical_type: statisticalOption.value
    }
    socket.send(uploadData)
    handleTaskCallback.value(uploadData)
  }

  const handleTaskCallbackObj = {
    simpleCharts: (uploadData) => {
    },
    multipleTables: (uploadData) => {
      multipleTaskData.initMultipleTableData(uploadData)
    }
  }

  watch(
    () => route.name,
    (newVal) => {
      handleTaskFunc.value = handleTaskObj[newVal]
      handleTaskCallback.value = handleTaskCallbackObj[newVal]
    },
    { immediate: true }
  )

  return {
    handleUploadTask
  }
}
